package com.mikufufu.blog.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mikufufu.blog.common.entity.Query;
import com.mikufufu.blog.model.entity.Tag;
import com.mikufufu.blog.service.TagService;
import com.mikufufu.blog.common.entity.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/tag")
@Api(tags = "标签接口")
public class TagAdminController {

    @Autowired
    private TagService tagService;

    @GetMapping("/detail")
    @ApiOperation(value = "获取文章标签")
    @ApiImplicitParam(name = "id", value = "标签id", required = true)
    public  AjaxResult<Tag> detail(Integer id) {
        return AjaxResult.data(tagService.getById(id));
    }

    @GetMapping("/list")
    @ApiOperation(value = "获取文章标签列表")
    public AjaxResult<List<Tag>> list() {
        return AjaxResult.data(tagService.list());
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页获取文章标签列表")
    @ApiImplicitParam(name = "tagType", value = "标签类型（0.文章标签,1.相册标签）", required = true)
    public AjaxResult<IPage<Tag>> page(Query query,Integer tagType) {
        return AjaxResult.data(tagService.page(query.getPage(),new LambdaQueryWrapper<Tag>().eq(tagType!=null ,Tag::getTagType, tagType)));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增文章标签")
    public AjaxResult<Boolean> add(@RequestBody Tag tag) {
        return AjaxResult.status(tagService.save(tag));
    }

    @PutMapping("/update")
    @ApiOperation(value = "修改文章标签")
    public AjaxResult<Boolean> update(@RequestBody Tag tag) {
        return AjaxResult.status(tagService.updateById(tag));
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除文章标签")
    @ApiImplicitParam(name = "id", value = "标签id", required = true)
    public AjaxResult<Boolean> delete(Integer id) {
        return AjaxResult.status(tagService.removeById(id));
    }

}
